/**
 * Definition for isBadVersion()
 * 
 * @param {integer} version number
 * @return {boolean} whether the version is bad
 * isBadVersion = function(version) {
 *     ...
 * };
 */

/**
 * @param {function} isBadVersion()
 * @return {function}
 */

/**
 * @description 二分法
 */

var solution = function (isBadVersion) {
    /**
     * @param {integer} n Total versions
     * @return {integer} The first bad version
     */
    return function (n) {
        let left = 1;
        let right = n;
        let mid = Math.floor(n / 2)
        while (left <= right) {
            if (!isBadVersion(mid)) {
                left = mid + 1
            } else {
                right = mid - 1
            }
            mid = right - Math.floor((right - left) / 2)
        }
        return mid
    };
};